import urllib2
from django.http import HttpResponse

REPORT_URL = "http://127.0.0.1:8080/ReportGenerator/genpdf"

def generateReport(data, pdfname, inline=True):
    try:
        responseReport = urllib2.urlopen(urllib2.Request(REPORT_URL, data, {'Content-Type': 'application/json', 'Content-Length': len(data)}))

        if responseReport.info().getheader('Content-Type') == "application/pdf":
            reportData = responseReport.read()
            response = HttpResponse(reportData)
            response['Content-Disposition'] = '%s; filename=%s' % ("inline" if inline else "attachment", pdfname)
            response["Content-Type"] = "application/pdf; charset=UTF-8"
            responseReport.close()
            return response
        elif responseReport.info().getheader('Content-Type') == "text/plain":
            return HttpResponse(responseReport.read())
        else:
            return HttpResponse("Error Desconocido")
    except Exception as e:
        return HttpResponse("No se pudo generar el reporte en formato PDF. " + e.message)
